package com.elysia.newsservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elysia.newsservice.domain.dto.GameNewsDTO;
import com.elysia.newsservice.domain.entity.GameNews;
import com.elysia.newsservice.domain.vo.GameNewsVO;
import com.elysia.newsservice.feign.FileFeignClient;
import com.elysia.newsservice.mapper.GameNewsMapper;
import com.elysia.newsservice.result.Result;
import com.elysia.newsservice.service.IGameNewsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class IGameNewsServiceImpl extends ServiceImpl<GameNewsMapper, GameNews> implements IGameNewsService {
    private final FileFeignClient fileFeignClient;

    /**
     * 添加公告
     * @param gameNewsDTO 添加的公告信息
     * @return 添加成功后的公告信息
     */
    @Override
    public Result<GameNewsVO> addGameNews(GameNewsDTO gameNewsDTO) {
        GameNews gameNews = GameNews.builder()
                .title(gameNewsDTO.getTitle())
                .updateDate(gameNewsDTO.getDate())
                .fileKey(gameNewsDTO.getFileKey())
                .summary(gameNewsDTO.getSummary())
                .build();
        save(gameNews);
        return Result.success(GameNewsVO.builder()
                .id(gameNews.getId())
                .title(gameNews.getTitle())
                .date(gameNews.getUpdateDate())
                .image(fileFeignClient.getFileUrl(gameNews.getFileKey(), 3600))
                .summary(gameNews.getSummary())
                .build());
    }

    /**
     * 获取所有公告
     */
    @Override
    public Result<List<GameNewsVO>> getAllGameNews() {
        List<GameNewsVO> result = new ArrayList<>();
        List<GameNews> gameNewsList = list();
        System.out.println(gameNewsList);
        for (GameNews gameNews : gameNewsList) {
            result.add(GameNewsVO.builder()
                    .id(gameNews.getId())
                    .title(gameNews.getTitle())
                    .date(gameNews.getUpdateDate())
                    .image(fileFeignClient.getFileUrl(gameNews.getFileKey(), 3600))
                    .summary(gameNews.getSummary())
                    .build());
        }
        return Result.success(result);
    }

    /*
    * 更新公告
    * @param id 所需更新公告的id
    * @param gameNewsDTO 更新的公告信息
     */
    @Override
    public Result<GameNewsVO> updateGameNews(Long id, GameNewsDTO gameNewsDTO) {
        GameNews gameNews = lambdaQuery().eq(GameNews::getId, id).one();
        gameNews.setTitle(gameNewsDTO.getTitle());
        gameNews.setUpdateDate(gameNewsDTO.getDate());
        // 前端可能不更新图片文件，所以这里要判断，避免多余操作
        if (gameNewsDTO.getFileKey() == null)
            gameNews.setFileKey(gameNews.getFileKey());
        // 如果更新了图片文件，则更新图片路径
        else {
            // 先删除原有的图片文件
            fileFeignClient.deleteFile(gameNews.getFileKey());
            gameNews.setFileKey(gameNewsDTO.getFileKey());
        }
        gameNews.setSummary(gameNewsDTO.getSummary());
        updateById(gameNews);
        return Result.success(GameNewsVO.builder()
                .id(gameNews.getId())
                .title(gameNews.getTitle())
                .date(gameNews.getUpdateDate())
                .image(fileFeignClient.getFileUrl(gameNews.getFileKey(), 3600))
                .summary(gameNews.getSummary())
                .build());
    }

    /**
     * 删除公告
     * @param id 删除的id
     * @return 删除结果
     */
    @Override
    public Result<String> deleteGameNews(Long id) {
        // 先获取公告的图片路径
        String imageKey = lambdaQuery().eq(GameNews::getId, id).one().getFileKey();
        // 删除公告
        removeById(id);
        // 删除图片
        fileFeignClient.deleteFile(imageKey);
        return Result.success();
    }
}
